/*
 * 文件名：LocalCouponServiceImpl.java	 
 * 时     间：下午5:39:04
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.local.dao.mapers.LocalCouponCashMapper;
import com.local.model.LocalCouponCash;
import com.local.model.LocalCouponCashExample;
import com.local.service.face.LocalCouponCashService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.Tools;
import com.qianhai.common.utils.UUIDGenerateUtil;

@Service
public class LocalCouponCashServiceImpl extends BaseServiceImpl<LocalCouponCashExample, LocalCouponCash, String> implements LocalCouponCashService
{
	@Autowired
	private LocalCouponCashMapper localCouponCashMapper;

	@Override
	public String saveLocalCouponCash(LocalCouponCash localCouponCash)
	{
		// TODO Auto-generated method stub
		try
		{
			localCouponCashMapper.deleteByPrimaryKey(localCouponCash.getCouponId());
			if(Tools.isEmpty(localCouponCash.getCouponId())){
				localCouponCash.setCouponId(UUIDGenerateUtil.generate());
			}
			SimpleDateFormat ss=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			if(Tools.isEmpty(localCouponCash.getCreateTime())){
				localCouponCash.setCreateTime(new Date(System.currentTimeMillis()));
				localCouponCash.setUseTime(!Tools.isEmpty(ss.format(localCouponCash.getCreateTime()))?ss.format(localCouponCash.getCreateTime()):ss.format(new Date()));
			}
			localCouponCashMapper.insert(localCouponCash);
		} catch (Throwable e)
		{
			throw new BaseException(e.getMessage());
		}
		return localCouponCash.getCouponId();
	}

	@Override
	public BaseMapper<LocalCouponCashExample, LocalCouponCash, String> getMapper()
	{
		// TODO Auto-generated method stub
		return localCouponCashMapper;
	}

	
	/**
	 * 通过车牌号
	 * 找到优惠券集合
	 */
	@Override
	public List<LocalCouponCash> queryCouponCashByCarNo(String carNo)
	{
		try
		{
			LocalCouponCashExample contdion = new LocalCouponCashExample();
			contdion.or().andCarNoEqualTo(carNo).andCouponTypeEqualTo(1);
			List<LocalCouponCash> li=localCouponCashMapper.selectByCondition(contdion);
			if(!Tools.isEmpty(li)&&li.size()>0){
				/*//根据price 属性，进行排序
				Collections.sort(li, new Comparator<LocalCouponCash>(){  
		            public int compare(LocalCouponCash o1, LocalCouponCash o2) {  
		              
		                //按照价格进行降序排列  
		                if(o1.getPrice() > o2.getPrice()){  
		                    return 1;  
		                }  
		                if(o1.getPrice() == o2.getPrice()){  
		                    return 0;  
		                }  
		                return -1;  
		            }  
		        }); */
				return li;
			}
			return null;
		} catch (Exception e)
		{
			return null;
		}
	}

	/**
	 * 删除优惠卷中，车牌号的集合
	 */
	@Override
	public void deleteCouponCashByCarNo(String carNo)
	{
		// TODO Auto-generated method stub
		LocalCouponCashExample contdion = new LocalCouponCashExample();
		contdion.or().andCarNoEqualTo(carNo);
		localCouponCashMapper.deleteByCondition(contdion);
	}
}
